﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IronOne.SalesOne.BusinessObjects;
using Facade;
using System.Data;
using System.Configuration;


namespace IronOne.SalesOne.SalesOneWeb.Structure
{
    public partial class ManageUnits : SOSecurityPage, IDataPage, IntTest
    {
        public List<SalesOneUnit> unitList;
        private int isChecked = 0;
        private static int pageNo = 1;
        private int nodeId;


        protected void Page_Load(object sender, EventArgs e)
        {

            





            string postCause = this.Page.Request.Params.Get("__EVENTTARGET");

            if (string.IsNullOrEmpty(postCause))
            {
                postCause = "REDIRECT";
            }




            //lblHeader1.Text = "Manage Unit";
            //lblHeader1.Visible = false;
            Session["usrType"] = "Manage Unit";
            btnManageUsers.Visible = false;
            btnCreate.Visible = true;

            if (Session["sccUnit"] != null)
            {
                MassageSummery("IIS401");
                //lblResult.Visible = true;
                //lblResult.Text = Session["sccUnit"].ToString();
                //lblResult.ForeColor = System.Drawing.Color.Green;
                Session.Remove("sccUnit");
            }

            if (Session["failUnit"] != null)
            {

                MassageSummery("IIS502");
                //lblResult.Visible = true;
                //lblResult.Text = Session["failUnit"].ToString();
                //lblResult.ForeColor = System.Drawing.Color.Red;
                Session.Remove("failUnit");
            }

            if (Session["sccUnitIn"] != null)
            {
                MassageSummery("IIS404");
                //lblResult.Visible = true;
                //lblResult.Text = Session["sccUnit"].ToString();
                //lblResult.ForeColor = System.Drawing.Color.Green;
                Session.Remove("sccUnitIn");
            }

            if (Session["failUnitIn"] != null)
            {
                MassageSummery("IIS502");
                //lblResult.Visible = true;
                //lblResult.Text = Session["sccUnit"].ToString();
                //lblResult.ForeColor = System.Drawing.Color.Green;
                Session.Remove("failUnitIn");
            }





            GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

            UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));


            DropDownList drBrnch = (DropDownList)UCSearch1.FindControl("drpdownBranch");
            DropDownList drGroup = (DropDownList)UCSearch1.FindControl("drpdownGroup");



            if (!IsPostBack)
            {
                try
                {


                    if (Page.Request.Params["brnchId"].ToString() != null)
                    {
                        StructureFacade fac = new StructureFacade();


                        int bId = Convert.ToInt32(Page.Request.Params["brnchId"].ToString());



                        nodeId = bId;
                        //UCGrid1.Visible = false;
                        btnCreate.Visible = false;

                        drGroup.SelectedValue = fac.GetBranch(bId).GroupId.ToString();

                        List<Branch> br = fac.GetBranchesForGroup(fac.GetBranch(bId).GroupId);
                        drBrnch.DataSource = br;
                        Branch brSel = new Branch();
                        brSel.BranchId = -1;
                        brSel.BranchName = "Select One...";
                        br.Insert(0, brSel);
                        drBrnch.DataTextField = "BranchName";
                        drBrnch.DataValueField = "BranchId";
                        drBrnch.DataBind();
                        drBrnch.SelectedValue = bId.ToString();



                        lblResult.Visible = false;
                        btnManageUsers.Visible = true;
                        UCGridButtons1.Visible = true;

                        int[] numbers = { 1,2,3 };
                        System.Web.UI.HtmlControls.HtmlInputHidden loadGrd = (System.Web.UI.HtmlControls.HtmlInputHidden)UCSearch1.FindControl("_SETGRIDSOURCE");
                        // loadGrd.Value = "1";
                        //if (loadGrd.Value == "1")
                        // {
                        // btnManageUsers.Visible = true;
                        // UCGrid1.SetDataSource(Source(bId), numbers);
                        // loadGrd.Value = "0";
                        //}

                        unitList = Source(bId, 1);
                        if (unitList.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                        {
                            UCGrid1.IsLastPage = (Source(bId, 2)).Count > 0 ? false : true;
                        }

                        pageNo = 1;
                        UCGrid1.SetText("1");

                        if (postCause.Contains("lbtNext"))
                        {
                            if (UCGrid1.GetText() != "1")
                            {
                                pageNo = Convert.ToInt32(UCGrid1.GetText() + 1);
                                UCGrid1.SetText(pageNo.ToString());

                            }
                            else
                            {
                                pageNo = 2;
                                UCGrid1.SetText("2");

                            }

                            unitList = Source(nodeId, pageNo);
                            if (unitList.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                            {
                                UCGrid1.IsLastPage = (Source(nodeId, pageNo + 1)).Count > 0 ? false : true;
                            }
                        }
                        else if (postCause.Contains("lbtPrev"))
                        {
                            if (UCGrid1.GetText() != "1")
                            {
                                pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
                                UCGrid1.SetText(pageNo.ToString());

                                unitList = Source(nodeId, pageNo);
                                if (unitList.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(nodeId, pageNo + 1)).Count > 0 ? false : true;
                                }
                            }
                        }
                        else if (postCause.Contains("lbtGo"))
                        {
                            int p = int.Parse(UCGrid1.GetText());
                            if (p != 0)
                            {
                                try
                                {
                                    unitList = Source(nodeId, p);
                                    if (unitList.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                    {
                                        UCGrid1.IsLastPage = (Source(nodeId, p + 1)).Count > 0 ? false : true;
                                    }
                                    pageNo = p;
                                }
                                catch (Exception)
                                {

                                    throw;
                                }


                            }

                        }

                        if (!postCause.Contains("RadioButton1"))
                        {
                            try
                            {
                                if (unitList.Count != 0)
                                {
                                    lblResult.Visible = false;

                                    UCGrid1.styleList = new List<GridViewStyle> {
                new GridViewStyle{
                    ColumnId = "Name",
                    ColumnHeader = "Unit Name",
                    ColumnWidth = 130},
                new GridViewStyle{
                    ColumnId = "From",
                    ColumnHeader = "Effective From",
                    ColumnWidth = 130},
                new GridViewStyle{
                    ColumnId = "Code",
                    ColumnHeader = "Code",
                    ColumnWidth = 80},
                new GridViewStyle{
                    ColumnId = "To",
                    ColumnHeader = "Effective To",
                    ColumnWidth = 130}
                
            };






                                    UCGrid1.SetDataSource(unitList, numbers);
                                    Session.Remove("branchId");
                                }
                                else
                                {
                                    btnCreate.Visible = true;
                                    //Session.Remove("unitId");

                                    MassageSummery("IIS504");

                                    //lblResult.Visible = true;
                                    //lblResult.Text = "No Results to be displayed";
                                    //lblResult.ForeColor = System.Drawing.Color.Red;
                                    UCGridButtons1.Visible = false;
                                    UCGrid1.Visible = false;
                                    btnManageUsers.Visible = false;
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                        //Session.Remove("branchId");




                    }

                }
                catch (Exception es)
                {
                    UCGridButtons1.Visible = false;
                }
            }




            //if (Session["branchId"] != null)
            //{
            //    StructureFacade fac = new StructureFacade();
            //    int bId = Convert.ToInt32(Session["branchId"].ToString());
            //    nodeId = bId;
            //    //UCGrid1.Visible = false;
            //    btnCreate.Visible = false;

            //    drGroup.SelectedValue = fac.GetBranch(bId).GroupId.ToString();

            //    List<Branch> br = fac.GetBranchesForGroup(fac.GetBranch(bId).GroupId);
            //    drBrnch.DataSource = br;
            //    Branch brSel = new Branch();
            //    brSel.BranchId = -1;
            //    brSel.BranchName = "Select One...";
            //    br.Insert(0, brSel);
            //    drBrnch.DataTextField = "BranchName";
            //    drBrnch.DataValueField = "BranchId";
            //    drBrnch.DataBind();
            //    drBrnch.SelectedValue = bId.ToString();



            //    lblResult.Visible = false;
            //    btnManageUsers.Visible = true;
            //    UCGridButtons1.Visible = true;

            //    int[] numbers = { 1, 5 };
            //    System.Web.UI.HtmlControls.HtmlInputHidden loadGrd = (System.Web.UI.HtmlControls.HtmlInputHidden)UCSearch1.FindControl("_SETGRIDSOURCE");
            //    // loadGrd.Value = "1";
            //    //if (loadGrd.Value == "1")
            //    // {
            //    // btnManageUsers.Visible = true;
            //    // UCGrid1.SetDataSource(Source(bId), numbers);
            //    // loadGrd.Value = "0";
            //    //}

            //    unitList = Source(bId, 1);
            //    pageNo = 1;
            //    UCGrid1.SetText("1");

            //   if (postCause.Contains("lbtNext"))
            //    {
            //        if (UCGrid1.GetText() != "1")
            //        {
            //            pageNo = Convert.ToInt32(UCGrid1.GetText() + 1);
            //            UCGrid1.SetText(pageNo.ToString());

            //        }
            //        else
            //        {
            //            pageNo = 2;
            //            UCGrid1.SetText("2");

            //        }

            //        unitList = Source(nodeId, pageNo);

            //    }
            //    else if (postCause.Contains("lbtPrev"))
            //    {
            //        if (UCGrid1.GetText() != "1")
            //        {
            //            pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
            //            UCGrid1.SetText(pageNo.ToString());

            //            unitList = Source(nodeId, pageNo);
            //        }
            //    }
            //    else if (postCause.Contains("lbtGo"))
            //    {
            //        int p = int.Parse(UCGrid1.GetText());
            //        if (p != 0)
            //        {
            //            try
            //            {
            //                unitList = Source(nodeId, p);
            //                pageNo = p;
            //            }
            //            catch (Exception)
            //            {

            //                throw;
            //            }


            //        }

            //    }

            //    if (!postCause.Contains("RadioButton1"))
            //    {
            //        try
            //        {
            //            if (unitList.Count != 0)
            //            {
            //                lblResult.Visible = false;
            //                UCGrid1.SetDataSource(unitList, numbers);
            //                Session.Remove("branchId");
            //            }
            //            else
            //            {
            //                btnCreate.Visible = true;
            //                //Session.Remove("unitId");

            //                MassageSummery("IIS504");

            //                //lblResult.Visible = true;
            //                //lblResult.Text = "No Results to be displayed";
            //                //lblResult.ForeColor = System.Drawing.Color.Red;
            //                UCGridButtons1.Visible = false;
            //                UCGrid1.Visible = false;
            //                btnManageUsers.Visible = false;
            //            }
            //        }
            //        catch (Exception ex)
            //        {
            //        }
            //    }
            //    //Session.Remove("branchId");




            //}






            else if (drBrnch != null && drBrnch.SelectedValue != "-1" && drGroup != null)
            {
                Session.Remove("branchId");
                UCGrid1.Visible = true;
                //unitList.Clear();

                btnCreate.Visible = false;
                //lblResult.Visible = true;
                lblResult.Visible = false;
                btnManageUsers.Visible = true;
                UCGridButtons1.Visible = true;
                int brnchId = Convert.ToInt32(drBrnch.SelectedValue);
                nodeId = brnchId;
                int[] numbers = { 1, 2, 3 };
                //System.Web.UI.HtmlControls.HtmlInputHidden loadGrd = (System.Web.UI.HtmlControls.HtmlInputHidden)UCSearch1.FindControl("_SETGRIDSOURCE");
                //if (loadGrd.Value == "1")
                //{
                btnManageUsers.Visible = true;
                //unitList = Source(brnchId);

                if (postCause.Contains("drpdownBranch"))
                {
                    pageNo = 1;
                    UCGrid1.SetText("1");
                    unitList = Source(brnchId, 1);
                    if (unitList.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                    {
                        UCGrid1.IsLastPage = (Source(brnchId, 2)).Count > 0 ? false : true;
                    }
                }
                else if (postCause.Contains("lbtNext"))
                {
                    if (UCGrid1.GetText() != "1")
                    {
                        pageNo = Convert.ToInt32(UCGrid1.GetText() + 1);
                        UCGrid1.SetText(pageNo.ToString());

                    }
                    else
                    {
                        pageNo = 2;
                        UCGrid1.SetText("2");

                    }

                    unitList = Source(nodeId, pageNo);
                    if (unitList.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                    {
                        UCGrid1.IsLastPage = (Source(nodeId, pageNo + 1)).Count > 0 ? false : true;
                    }
                }
                else if (postCause.Contains("lbtPrev"))
                {
                    if (UCGrid1.GetText() != "1")
                    {
                        pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
                        UCGrid1.SetText(pageNo.ToString());

                        unitList = Source(nodeId, pageNo);
                        if (unitList.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                        {
                            UCGrid1.IsLastPage = (Source(nodeId, pageNo + 1)).Count > 0 ? false : true;
                        }
                    }
                }
                else if (postCause.Contains("lbtGo"))
                {
                    int p = int.Parse(UCGrid1.GetText());
                    if (p != 0)
                    {
                        try
                        {
                            unitList = Source(nodeId, p);
                            if (unitList.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                            {
                                UCGrid1.IsLastPage = (Source(nodeId, p + 1)).Count > 0 ? false : true;
                            }
                            pageNo = p;
                        }
                        catch (Exception)
                        {

                            throw;
                        }


                    }

                }


                if (!postCause.Contains("RadioButton1"))
                {
                    try
                    {
                        if (unitList.Count != 0)
                        {
                            lblResult.Visible = false;

                            UCGrid1.styleList = new List<GridViewStyle> {
                new GridViewStyle{
                    ColumnId = "Name",
                    ColumnHeader = "Unit Name",
                    ColumnWidth = 130},
                new GridViewStyle{
                    ColumnId = "From",
                    ColumnHeader = "Effective From",
                    ColumnWidth = 130},
               new GridViewStyle{
                    ColumnId = "Code",
                    ColumnHeader = "Code",
                    ColumnWidth = 80},
                new GridViewStyle{
                    ColumnId = "To",
                    ColumnHeader = "Effective To",
                    ColumnWidth = 130}
                
            };




                            UCGrid1.SetDataSource(unitList, numbers);
                            btnCreate.Visible = false;

                            //Session.Remove("branchId");
                            //UCGrid1.SetDataSource(sou);
                        }
                        else
                        {
                            btnCreate.Visible = true;


                            //Session.Remove("unitId");

                            MassageSummery("IIS504");

                            //lblResult.Visible = true;
                            //lblResult.Text = "No Results to be displayed";
                            //lblResult.ForeColor = System.Drawing.Color.Red;
                            UCGridButtons1.Visible = false;
                            UCGrid1.Visible = false;
                            btnManageUsers.Visible = false;
                        }
                    }
                    catch (Exception ex) 
                    {
                        UCGrid1.Visible = false;
                        UCGridButtons1.Visible = false;
                        btnManageUsers.Visible = false;
                        btnCreate.Visible = true;
                    }


                    //Session.Remove("branchId");
                }

                //Session.Remove("branchId");

                // loadGrd.Value = "0";

                //}
            }
            else
            {
                UCGridButtons1.Visible = false;
            }

        }




        //private List<SalesOneUnit> Source(int nodeId)
        //{
        // StructureFacade fac = new StructureFacade();
        // return fac.GetUnitsForBranch(nodeId);

        //}

        private List<SalesOneUnit> Source(int nodeId, int pageNo)
        {
            StructureFacade fac = new StructureFacade();
            return fac.GetUnitsForBranch(nodeId, pageNo);

        }

        #region IDataPage Members

        public void Create()
        {

            GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

            foreach (GridViewRow row in gr.Rows)
            {
                RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                if (rbtn.Checked)
                {
                    Response.Redirect("~/Structure/AddUnit.aspx?unitid_c=" + row.Cells[2].Text + "&branchid_c=" + row.Cells[3].Text);
                }
                else
                {
                    MassageSummery("IIS503");
                }
            }  




           // Response.Redirect("~/Structure/AddUnit.aspx");
        }

        public void View()
        {

            GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

            foreach (GridViewRow row in gr.Rows)
            {
                RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                if (rbtn.Checked)
                {
                    Response.Redirect("~/Structure/AddUnit.aspx?a=v&unitid_v=" + row.Cells[2].Text);
                }
                else
                {
                    MassageSummery("IIS503");
                }
            } 


            //if (Session["unitId"] == null)
            //{

            //    Session["unitId"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
            //    Session["BID"] = ((GridViewRow)Session["firstRow"]).Cells[3].Text;

            //    string bId = Session["BID"].ToString();


            //    Session.Remove("firstRow");
            //}

            //if (Session["unitId"] != null)
            //{
            //    Response.Redirect("~/Structure/AddUnit.aspx?a=v");
            //}
            //else
            //{
            //    //lblResult.Visible = true;
            //    //lblResult.Text = "Please Select a unit to be viewed";
            //    //lblResult.ForeColor = System.Drawing.Color.Red;

            //    MassageSummery("IIS503");

            //}
        }

        public void Update()
        {
           // Session["branchId"] = ((GridViewRow)Session["firstRow"]).Cells[3].Text;
            //if (Session["unitId"] == null)
            //{

            //    Session["unitId"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
            //    Session["BID"] = ((GridViewRow)Session["firstRow"]).Cells[3].Text;

            //    string bId = Session["BID"].ToString();

            //    Session.Remove("firstRow");
            //}

            ////string unitId = Session["unitId"].ToString();

            ////string branchId = Session["branchId"].ToString();
            //if (Session["unitId"] != null && Session["BID"] != null)
            //{
            //    Response.Redirect("~/Structure/AddUnit.aspx?a=u");
            //}
            //else
            //{
            //    MassageSummery("IIS503");

            //    //lblResult.Visible = true;
            //    //lblResult.Text = "Please Select a unit to be updated";
            //    //lblResult.ForeColor = System.Drawing.Color.Red;
            //}


            GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

            foreach (GridViewRow row in gr.Rows)
            {
                RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                if (rbtn.Checked)
                {
                    Response.Redirect("~/Structure/AddUnit.aspx?a=u&unitid_u=" + row.Cells[2].Text + "&branchid_u=" + row.Cells[3].Text);
                }
                else
                {
                    MassageSummery("IIS503");
                }
            } 





        }

        public void Delete()
        {
            //StructureFacade fac = new StructureFacade();
            //string unit = Session["unitId"].ToString();
            //SalesOneUnit soUnit = fac.GetUnit(Convert.ToInt32(unit));

            //fac.DeleteUnit(soUnit);
        }

        public void Cancel()
        {
            Response.Redirect("~/Structure/ManageUnits.aspx");


        }

        public void GetId(GridViewRow row)
        {
            Session["unitId"] = row.Cells[2].Text;
            Session["BID"] = row.Cells[3].Text;
            string bId = Session["BID"].ToString();
            lblResult.Visible = false;
        }

        #endregion



        

        protected void btnManageUsers_Click(object sender, EventArgs e)
        {
            #region commented
            //if (Session["unitId"] == null)
            //{

            //    Session["unitId"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
            //    Session["BID"] = ((GridViewRow)Session["firstRow"]).Cells[3].Text;

            //    string bId = Session["BID"].ToString();


            //    Session.Remove("firstRow");
            //}

            //if (Session["unitId"] != null)
            //{
            //    Response.Redirect("~/UserManagement/ManageUsers.aspx?x=u");
            //}
            //else
            //{

            //    MassageSummery("IIS503");

            //    //lblResult.Visible = true;
            //    //lblResult.Text = "Please Select a unit";
            //    //lblResult.ForeColor = System.Drawing.Color.Red;
            //} 
            #endregion


            GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

            foreach (GridViewRow row in gr.Rows)
            {
                RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                if (rbtn.Checked)
                {
                    Response.Redirect("~/UserManagement/ManageUsers.aspx?x=u&uId_m_u=" + row.Cells[2].Text);
                }
                else
                {
                    MassageSummery("IIS503");
                }
            }


        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {

            DropDownList drBrnch = (DropDownList)UCSearch1.FindControl("drpdownBranch");
            DropDownList drGroup = (DropDownList)UCSearch1.FindControl("drpdownGroup");

            string brch = drBrnch.SelectedValue.ToString();
            string grp = drGroup.SelectedValue.ToString();


            Response.Redirect("~/Structure/AddUnit.aspx?grp_id=" + grp + "&brch_id=" + brch);
        }





        #region IntTest Members

        public void Next()
        {
            //if (UCGrid1.GetText() != "1")
            //{
            // pageNo = Convert.ToInt32(UCGrid1.GetText() + 1);
            // UCGrid1.SetText(pageNo.ToString());

            //}
            //else
            //{
            // pageNo = 2;
            // UCGrid1.SetText("2");

            //}

            //unitList=Source(nodeId, pageNo);
        }

        public void Previous()
        {
            //if (UCGrid1.GetText() != "1")
            //{
            // pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
            // UCGrid1.SetText(pageNo.ToString());

            // unitList=Source(nodeId, pageNo);
            //}
        }

        public void Go()
        {
            //int p = int.Parse(UCGrid1.GetText());
            //if (p != 0)
            //{
            //    try
            //    {
            //        Source(nodeId, p);
            //        pageNo = p;
            //    }
            //    catch (Exception)
            //    {

            //        throw;
            //    }


            //}
        }

        #endregion
    }



}
